package cn.sfrog.project.biz.applet.controller;

import cn.sfrog.common.core.domain.ApiAjaxResult;
import cn.sfrog.common.utils.ip.AddressUtils;
import cn.sfrog.common.utils.ip.IpUtils;


import cn.sfrog.project.biz.applet.config.WxPayConfig;
import cn.sfrog.project.biz.applet.service.WxPayService;
import cn.sfrog.project.biz.applet.utils.HttpClientUtil;
import cn.sfrog.project.biz.applet.utils.WechatUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jfinal.ext.interceptor.GET;
import com.jthinking.common.util.ip.IPInfo;
import com.jthinking.common.util.ip.IPInfoUtils;
import com.sun.nio.sctp.NotificationHandler;
import com.wechat.pay.contrib.apache.httpclient.constant.WechatPayHttpHeaders;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import okhttp3.HttpUrl;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.management.Notification;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.*;

/**
 * @Author chenl
 * @Date 2023-12-25 15:36
 * @title TestWxConfig
 * @description
 **/
@RestController
@Slf4j
@RequestMapping("/test/pay")
public class TestWxConfig {
    @Autowired
    ApplicationContext applicationContext;
    @Autowired
    WxPayConfig wxPayConfig;
    @Autowired
    WxPayService wxPayService;
    @ApiOperation("调用统一下单API,jsapi")
    @GetMapping("/test")
    public void test1() throws Exception {
        log.info("发起请求");
//        JSONObject sessionKeyOrOpenId = WechatUtil.getSessionKeyOrOpenId("0d3SMG1w3jmo423r5F0w3E4wIB3SMG15",wxPayConfig.getAppid(), "7d53a592ec1f2a6a336acd7daed64987", "authorization_code");
//        System.out.println("openids"+sessionKeyOrOpenId);
//        System.out.println(sessionKeyOrOpenId.get("openid").toString());
        Map<String,Object> map = wxPayService.jsapi("oiWN569j2NwwaSjnR6BI_03PzYxg");
        System.out.println(map);
    }
    @ApiOperation("调用统一下单API,App")
    @GetMapping("/test1")
    public String test(HttpServletRequest request) throws Exception {
        System.out.println(request.getHeader("Wechatpay-Signature"));
        return request.getHeader("Wechatpay-Signature");
//        Resource resource = applicationContext.getResource("classpath:city.json");
//        String jsonStr = StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8);
//        JSONObject result = JSONObject.parseObject(jsonStr);
//        Object data = result.get("data");
//        JSONObject jsonObject = JSONObject.parseObject(data.toString());
//        Object cities = jsonObject.get("cities");
//        JSONArray array = JSON.parseArray(cities.toString());
//        ObjectMapper mapper = new ObjectMapper();
//        JsonNode root = mapper.readTree(jsonObject.get("cities").toString());
//
//        Iterator<JsonNode> elements = root.elements();
//        while (elements.hasNext()) {
//            JsonNode element = elements.next();
////            System.out.println(element.findValue("name").asText());
////            System.out.println(element);
//            if ("天津".equals(element.findValue("name").asText())) {
//                int cityId = element.findValue("cityId").asInt();
//                System.out.println(cityId);
//
//            }
//        }
//        String s = HttpClientUtil.doGet("https://api.ipify.org/?format=json");
//        IPInfo ipInfo = IPInfoUtils.getIpInfo("117.62.53.117");
//        System.out.println(ipInfo);
//        HashMap<String,String> hashMap = new HashMap<>();
//        String s1 = HttpClientUtil.doGet(AddressUtils.IP_URL);
//        String[] split = s1.split(",");
//        String[] split1 = split[3].split(":");
//        String[] split2 = split[4].split(":");
//        hashMap.put(split1[0].replace("\"", ""),split1[1].replace("\"", ""));
//        hashMap.put(split2[0].replace("\"", ""),split2[1].replace("\"", ""));
//        System.out.println(hashMap.get("city"));
        // 4-5

//        System.out.println(split);
//        System.out.println("s1:"+s1);
//        HashMap<String,String> headers = new HashMap<>();
//        headers.put("X-Host","mall.film-ticket.city.list");
//        JSONObject jsonObject = JSON.parseObject(HttpClientUtil.doGet("https://m.maizuo.com/gateway/", null, headers));
//        System.out.println(jsonObject);
        //        return ApiAjaxResult.success(jsonObject);
//        log.info("发起请求");
//        Map<String,Object> map = wxPayService.App();
    }

}
